package com.morning.dp.tools.demo;

import com.morning.dp.tools.dp.DPObjectBuilder;
import lombok.Data;

/**
 * 构建者模式
 *
 * @author @阿秋
 * @date 2021/3/19
 */
public class BuilderDemo {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        ComplexObject complexObject = DPObjectBuilder.startBuild(ComplexObject.class)
                .withParam("param1", "字段1")
                .withParam("param2", "字段2")
                .withParam("param3", "字段3")
                .withParam("param4", "字段4")
                .withParam("param5", "字段5")
                .withParam("param6", "字段6")
                .withParam("param7", "字段7")
                // 字段1构建逻辑
                .build(condition1(), BuilderDemo::buildField1)
                // 字段2构建逻辑
                .build(BuilderDemo::buildField2)
                // TODO：字段3构建条件不满足，将不会执行
                .build(condition2(), BuilderDemo::buildField3)
                // 字段4构建逻辑
                .build(BuilderDemo::buildField4)
                // 字段5构建逻辑
                .build(BuilderDemo::buildField5)
                // 字段6构建逻辑
                .build(BuilderDemo::buildField6)
                // 字段7构建逻辑
                .build(BuilderDemo::buildField7)
                // 获取最终结果
                .get();
        System.out.println(complexObject);

    }

    private static void buildField1(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段1。。。。。");
        String param1 = context.getParam("param1", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField1(param1 + "-构建完成");
        System.out.println("构建复杂字段1结束");
    }

    private static void buildField2(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段2。。。。。");
        String param2 = context.getParam("param2", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField2(param2 + "-构建完成");
        System.out.println("构建复杂字段2结束");
    }

    private static void buildField3(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段3。。。。。");
        String param3 = context.getParam("param3", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField3(param3 + "-构建完成");
        System.out.println("构建复杂字段3结束");
    }

    private static void buildField4(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段4。。。。。");
        String param4 = context.getParam("param4", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField4(param4 + "-构建完成");
        System.out.println("构建复杂字段4结束");
    }

    private static void buildField5(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段5。。。。。");
        String param5 = context.getParam("param5", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField5(param5 + "-构建完成");
        System.out.println("构建复杂字段5结束");
    }

    private static void buildField6(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段6。。。。。");
        String param6 = context.getParam("param6", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField6(param6 + "-构建完成");
        System.out.println("构建复杂字段6结束");
    }

    private static void buildField7(DPObjectBuilder.ObjectContext<ComplexObject> context) {
        System.out.println("构建复杂字段7。。。。。");
        String param7 = context.getParam("param7", String.class);
        ComplexObject target = context.getTarget();
        target.setComplexField7(param7 + "-构建完成");
        System.out.println("构建复杂字段7结束");
    }

    private static boolean condition1() {
        return true;
    }

    private static boolean condition2() {
        return false;
    }

    /**
     * 复杂对象模拟
     */
    @Data
    public static class ComplexObject {
        private Object complexField1;
        private Object complexField2;
        private Object complexField3;
        private Object complexField4;
        private Object complexField5;
        private Object complexField6;
        private Object complexField7;
    }
}
